global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A27_industry_employment.log, replace name(tabl)

capture noi {  

	*table_industry_employment
	*Configuration
	global cvers "_ipc4"
	local min_pats 0 

	if (`min_pats' != 0) local restr "_min`min_pats'pats"

	use ${final_dir}/ind6090_patents_regression_data${cvers}.dta, clear


	capture program drop addMean
	program define addMean
		qui sum `1' [aw=avwt]
		local x: di %10.2f `r(mean)'
		estadd local weighted_mean `x'
	end

	* All industries - using 

	keep if in_relevant_field_use >= `min_pats'

	*Top panel, col 1) delta log employment | share auto using industry
	reg dlog_emp_alm sh_auto95_use computeruse [aw=avwt], vce(cluster uas_ind6090)
	addMean dqsts
	estadd local obs "{\num{`e(N)'}}"
	estimates store emp_col1

	*Top panel, col 5) delta log lowskill employment | share auto using industry
	reg dlog_lskilled sh_auto95_use computeruse [aw=avwt], vce(cluster uas_ind6090)
	addMean dqfing
	estadd local obs "{\num{`e(N)'}}"
	estimates store empl_col1

	*Top panel col 3) delta log highskill employment | share auto using industry
	reg dlog_hskilled sh_auto95_use computeruse [aw=avwt], vce(cluster uas_ind6090)
	addMean dratioHL
	estadd local obs "{\num{`e(N)'}}"
	estimates store emph_col1

	* All industries - using + making

	*Top panel col 2) delta log employment | share auto using industry + making industry
	reg dlog_emp_alm sh_auto95_use sh_auto95_mk computeruse [aw=avwt], vce(cluster uas_ind6090)
	addMean dqsts
	estadd local obs "{\num{`e(N)'}}"
	estimates store emp_col3

	*Top panel col 6) delta log lowskill employment | share auto using industry + making industry
	reg dlog_lskilled sh_auto95_use sh_auto95_mk computeruse [aw=avwt], vce(cluster uas_ind6090)
	addMean dqfing
	estadd local obs "{\num{`e(N)'}}"
	estimates store empl_col3

	*Top panel col 4) delta log highskill employment | share auto using industry + making industry
	reg dlog_hskilled sh_auto95_use sh_auto95_mk computeruse [aw=avwt], vce(cluster uas_ind6090)
	addMean dratioHL
	estadd local obs "{\num{`e(N)'}}"
	estimates store emph_col3

	* Manufacutring industries - using

	keep if manuf == 1

	*Bottom panel col 1) delta log employment | share auto using industry
	reg dlog_emp_alm sh_auto95_use computeruse [aw=avwt], vce(cluster uas_ind6090)
	addMean dqsts
	estadd local obs "{\num{`e(N)'}}"
	estimates store emp_colm

	*Bottom panel col 5) delta log lowskill employment | share auto using industry
	reg dlog_lskilled sh_auto95_use computeruse [aw=avwt], vce(cluster uas_ind6090)
	addMean dqfing
	estadd local obs "{\num{`e(N)'}}"
	estimates store empl_colm

	*Bottom panel col 3) delta log highskill employment | share auto using industry
	reg dlog_hskilled sh_auto95_use computeruse [aw=avwt], vce(cluster uas_ind6090)
	addMean dratioHL
	estadd local obs "{\num{`e(N)'}}"
	estimates store emph_colm

	* Manufacutring industries - using + making

	*Bottom panel col 2) delta log employment | share auto using industry + making industry
	reg dlog_emp_alm sh_auto95_use sh_auto95_mk computeruse [aw=avwt], vce(cluster uas_ind6090)
	addMean dqsts
	estadd local obs "{\num{`e(N)'}}"
	estimates store emp_colm3

	*Bottom panel col 6) delta log lowskill employment | share auto using industry + making industry
	reg dlog_lskilled sh_auto95_use sh_auto95_mk computeruse [aw=avwt], vce(cluster uas_ind6090)
	addMean dqfing
	estadd local obs "{\num{`e(N)'}}"
	estimates store empl_colm3

	*Bottom panel col 4) delta log highskill employment | share auto using industry + making industry
	reg dlog_hskilled sh_auto95_use sh_auto95_mk computeruse [aw=avwt], vce(cluster uas_ind6090)
	addMean dratioHL
	estadd local obs "{\num{`e(N)'}}"
	estimates store emph_colm3

	* Output table all industries
	qui do ${code_dir}/config/tabletools.do
	setlabels
	dhoztab emp_col1 emp_col3 emph_col1 emph_col3 empl_col1 empl_col3 using ${tab_dir}/appendix/Table_A27_emp1.tex,  b(%10.2f) se(%10.2f) nogaps  replace nolines numbers nonotes nolines showdepvarinfo nomtitles sfmt(%10.2f a1) scalars("r2 \noalign{\vskip 1.8mm} R$^2$" "weighted_mean Mean dependent variable" "obs Industries") label mgroups("$\Delta$ Log employment" "$\Delta$ Log high-skilled" "$\Delta$ Log low-skilled", pattern(1 0 1 0 1 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) drop(_cons) noobs order(sh_auto_use sh_auto_mk computeruse) rename(sh_auto95_use sh_auto_use sh_auto95_mk sh_auto_mk) ///
		depvar("none") 

	dhoztab emp_colm emp_colm3 emph_colm emph_colm3 empl_colm empl_colm3 using ${tab_dir}/appendix/Table_27_emp2.tex,  b(%10.2f) se(%10.2f) nogaps  replace nolines numbers nonotes nolines showdepvarinfo nomtitles sfmt(%10.2f a1) scalars("r2 \noalign{\vskip 1.8mm} R$^2$" "weighted_mean Mean dependent variable" "obs Industries") label mgroups("$\Delta$ Log employment" "$\Delta$ Log high-skilled" "$\Delta$ Log low-skilled", pattern(1 0 1 0 1 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) drop(_cons) noobs order(sh_auto_use sh_auto_mk computeruse) rename(sh_auto95_use sh_auto_use sh_auto95_mk sh_auto_mk) ///
	depvar("none") ///
		notes("This table shows the effect of automation technologies on employment. Each column represents a separate OLS regression of the change in log employment between 1980 and 1998 on the share of automation patents in machinery, the annual percentage point change in industry computer use during 1984-1997, and a constant. Panel A considers all industries. Panel B focuses on industries in manufacturing. In columns 1--2 the dependent variable is the change in log employment, in columns 2--3 the change in log employment of high-skilled workers (college graduates), and in columns 3--4 the change in log employment of low-skilled workers (others). The two automation share measures correspond to a different mapping between C/IPC codes and industries. Using industries allocates patents to their sector of use while innovating industry -- added in columns 2,4, and 6 -- allocates patents to their sector of invention. The regressions are weighed by the mean industry share of total employment in FTEs in 1980 and 1998. Standard errors are clustered at the level of industry groups that have the same automation share by construction and reported in parentheses.")

	* TODO: Note: they're pasted together manually in two tables currently
	*again } brackets
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}